//获取地址栏参数
function getParams() {
    var name, value;
    var str = location.href;
    var num = str.indexOf("?");
    str = str.substr(num + 1);
    var arr = str.split("&");
    var request = {};
    for (var i = 0; i < arr.length; i++) {
        num = arr[i].indexOf("=");
        if (num > 0) {
            name = arr[i].substring(0, num);
            value = decodeURIComponent(arr[i].substr(num + 1));
            request[name] = value;
            //console.log(request[name]);
        }
    }
    return request;
}

//根据name做判断，数组去重。arr是要过滤的数据，arrField是要对比的数组中的字段
function unique(arr,arrField) {
    //遍历arr,同时创建结果数组result
    for (var i = 0, result = []; i < arr.length; i++) {
        //遍历结果数组result
        for (var j = 0; j < result.length; j++) {
            //如果result中有一个元素等于arr[i],就退出循环
            //说明arr[i]，是一个重复的元素
            if (result[j][arrField] === arr[i][arrField]) {
                layer.msg('请勿提交重复数据，请检查数据！', { time: 1000 });
                return false;
            }
        }//遍历结束
        //如果j等于result的length，就把arr[i],压入数组result
        //j等于result的length,说明遍历到了最后，也就是没有找到相同的元素
        if (j === result.length) {
            result[result.length] = arr[i];
        }
    }
    return result;//返回result
}


//根据返回数据，改变数组的键值
// var data = [
//     { id: "1", roleId: "666", roleName: "系统管理员", roleDesc: "1" },
//     { id: "2", roleId: "999", roleName: "医务科", roleDesc: "1" }];

// //获取数组每个对象中data已有的键值的个数
// var fieldLen = Object.getOwnPropertyNames(data[0]);

function changeKeys(keys1, keys2, data) {
    var result = [];
    for (var row in data) {
        var r = {};
        for (var i = 0; i < keys1.length; i++) {
            var d = data[row];
            r[keys1[i]] = d[keys2[i]];

        }
        result.push(r);
    }
    return result;
}

// changeKeys(['id', 'uid', 'name', 'desc'], fieldLen, data);




//根据文件字节数，换算单位
function getFileSize(fileByte) {
    var fileSizeByte = fileByte;
    var fileSizeMsg = "";
    if (fileSizeByte < 1048576) fileSizeMsg = (fileSizeByte / 1024).toFixed(2) + "KB";
    else if (fileSizeByte == 1048576) fileSizeMsg = "1MB";
    else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824) fileSizeMsg = (fileSizeByte / (1024 * 1024)).toFixed(2) + "MB";
    else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824) fileSizeMsg = "1GB";
    else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776) fileSizeMsg = (fileSizeByte / (1024 * 1024 * 1024)).toFixed(2) + "GB";
    else fileSizeMsg = "文件超过1TB";
    return fileSizeMsg;
}
